package com.ficus.road.maintain.third.zh.roadgps;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.Coordinate;
import com.ficus.road.maintain.core.model.RoadInfo;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.zh.roadgps.TownGpsInfoZH;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/30 下午4:07
 */
@Component
public class ZHTownRoad extends BaseZH {

    @Autowired
    private RoadInfoDao roadInfoDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetPointByTownCode";
        arg1 = "{}";
        errMsg = "调用中海接口获取街镇道路异常!";
        successMsg = "调用中海接口获取街镇道路成功!";
    }

    //    @Scheduled(cron = "*/5 * * * * ?", zone = "GMT+8:00")
//    @Scheduled(cron = "0 30 3 10 * ?", zone = "GMT+8:00")
    public void refreshTownRoadInfo() {
        Date date = new Date();
        String townRoadStr = getZHData();
        if (ObjectUtils.isEmpty(townRoadStr)) {
            return;
        }
        List<TownGpsInfoZH> townGpsInfoZHList = JSONArray.parseArray(townRoadStr, TownGpsInfoZH.class);
        Map<String, RoadInfo> roadInfoMap = roadInfoDao.findAll().stream().collect(Collectors.toMap(RoadInfo::getId, item -> item));
        townGpsInfoZHList.stream().forEach(townGpsInfo -> {
            if (StringUtils.isNotBlank(townGpsInfo.getRoadId()) && StringUtils.isNotBlank(townGpsInfo.getTowncode())) {
                String id = townGpsInfo.getRoadId() + townGpsInfo.getTowncode();
                RoadInfo roadInfo = roadInfoMap.get(id);
                if (ObjectUtils.isEmpty(roadInfo)) {
                    roadInfo = new RoadInfo();
                    roadInfo.setId(id);
                }
                roadInfo.setRoadName(townGpsInfo.getRoadName());
                roadInfo.setId(townGpsInfo.getRoadId() + townGpsInfo.getTowncode());
                roadInfo.setRoadType(RoadTypeEnum.ROAD);
                roadInfo.setSecNum(townGpsInfo.getRoadId());
                roadInfo.setDistrictId(townGpsInfo.getTowncode());
                if (NumberUtils.isCreatable(townGpsInfo.getLength())) {
                    Double secLen = Double.valueOf(townGpsInfo.getLength());
                    roadInfo.setSecLen(String.valueOf(secLen / 1000));
                }
                roadInfo.setTown(townGpsInfo.getTown());
                String wgs84 = townGpsInfo.getGeojsonWgs84();
                TownGpsInfoZH.GeoJsonWGS84 geoJsonWGS84 = JSONObject.parseObject(wgs84, TownGpsInfoZH.GeoJsonWGS84.class);
                List<double[]> coordinatesZH = geoJsonWGS84.getGeometry().getCoordinates();
                List<Coordinate> coordinates = new ArrayList<>();
                coordinatesZH.forEach(array -> {
                    Coordinate coordinate = Coordinate.builder().x(array[0] + "").y(array[1] + "").build();
                    coordinates.add(coordinate);
                });
                roadInfo.setCoordinates(coordinates);
                roadInfo.setModificationTime(date);
                roadInfoDao.saveOrUpdate(roadInfo);
            }
        });
    }
}
